var ns;
var app = new Vue({
    el: '#app',
    data: {
        menus: [],
        chatlist: [],
        CategoryID: 0,
        is_searching: true,
        selectId:0,
        timeOutEvent: 0,
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getthreadlist',
                top: '20',
                CategoryID: that.CategoryID,
                type: 1,
                ThreadType:0,
                ThreadStatus:1
            }, function(succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    that.chatlist = data.chatlist;
                    setTimeout(function() {
                        that.menuFixed('top_menus');
                    }, 200)
                }
            });
        },
        get_menu: function() {
            var that = this;
            ns.post({
                action: 'getthreadlistmenu',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.menus = data.menus;
                    that.get_data();
                    setTimeout(function() {
                        that.menuFixed('top_menus');
                    }, 200)
                }
            });
        },
        //长按弹出操作框
        gettouchStart:function(index,id,UserID){
            var that = this;
            that.selectId=0;
            that.timeOutEvent=setTimeout(function(){
                ns.post({
                    UserID:UserID,
                    ThreadID:id,
                    action: 'CannotOperate',
                }, function(succeed, data, err) {
                    if(succeed){
                        if(data.status){
                            that.selectId=id;
                        }else{
                            api.toast({
                                msg: '不能进行此操作',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.removeEventListener({
                                name:'longpress'                       
                            })
                        }
                    }
                });

            },500)
        },
        gettouchmove:function(){
            var that=this;
            clearTimeout(that.timeOutEvent);
            that.timeOutEvent=0;
        },
        getttouchend:function(){
            var that=this;
            clearTimeout(that.timeOutEvent);
        },
        closebox:function(index,id){
            var that=this;
            that.selectId=0;
        },
        // 举报
        jubao:function(personId,id,ProjectID){
            var that=this;
            ns.openWin('dl_bbsjubao_frm', '举报',{
                ReportedUserID:personId,
                ThreadID:id,
                ProjectID:ProjectID,
                type:1,//1--举报人
                postType:2//1-拉黑，2-举报
            });
            that.selectId=0;
            
        },
        // 拉黑
        addto_black:function(personId,ProjectID){
            var that=this;
            that.selectId=0;
            api.confirm({
                title: '提示',
                msg: '拉黑用户后，您将无法查看和接受该用户的发布信息、回复信息。后续可以在【我的收藏-黑名单】中取消拉黑。是否确认操作？',
                buttons: ['确认拉黑', '取消操作']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.post({
                            Type:1,
                            ReportedUserID:personId,
                            ProjectID:ProjectID,
                            action: 'savamallreporttype',
                        }, function(succeed, data, err) {
                            if(succeed){
                                api.toast({
                                    msg: '已进入黑名单',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                api.sendEvent({
                                    name: 'updatedianzan'
                                });

                            }else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });
                    };
                };
            });
        },
        do_search_all: function() {
            var that = this;
            that.CategoryID = 0;
            that.get_data();
        },
        check_post_status: function(callback, title, name, id) {
            ns.post({
                action: 'checkuserpostthreadstatus',
            }, function(succeed, data, err) {
                if (succeed) {
                    if (typeof callback == "function") {
                        callback.call(this, title, name, id);
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        open_win: function(title, name, id, checkpost) {
            var that = this;
            if (name == '') {
                return;
            }
            id = id || 0;
            if (checkpost) {
                that.check_post_status(that.open_win, title, name, id);
                return;
            }
            ns.openWin(name, title, {
                title: title,
                url: name + '.html',
                id: id,
                jubao:1

            }, {
                needlogin: true
            });
        },
        menuFixed: function(id) {
            var that = this;
            var obj = document.getElementById(id);
            var height = obj.offsetTop;
            window.onscroll = function() {
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                var page_width = document.documentElement.offsetWidth || document.body.offsetWidth;
                if (scrollTop <= height) {
                    obj.style.position = 'static';
                    obj.style.left = 0;
                    obj.style.right = 0;
                    obj.style.width = '100%';
                } else {
                    obj.style.position = 'fixed';
                    obj.style.top = 0;
                    obj.style.left = '10px';
                    obj.style.right = '10px';
                    obj.style.width = (page_width - 20) + 'px';
                }
            }
        },
        do_search_baike: function(item) {
            var that = this;
            for (var i = 0; i < that.menus.length; i++) {
                that.menus[i].is_active = false;
            }
            item.is_active = true;
            that.CategoryID = item.id;
            that.get_data();
        },
        //收藏
        do_collectposts:function(item,type){
            var that=this;
            ns.post({
                action: 'postornot',
            }, function(succeed, data, err) {
                console.log(JSON.stringify(succeed));
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.IsActive){
                        ns.post({
                            id: item.id,
                            Type:type,
                            action: 'getcollectposts'
                        }, function(succeed, data, err){
                            if(succeed){
                                item.iscollection=data.IsCollection;
                            }
                        })
                    }else{
                        api.toast({
                            msg: "不能收藏",
                            duration: 2000,
                            location: 'bottom'
                        });

                    }

                }
            });
            
            
        },
        do_praise: function(item,type) {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function(succeed, data, err) {
                if(succeed){
                    if(data.IsActive){
                        that.do_post_thread(item,type);
                    }else{
                        api.toast({
                            msg: "不能点赞",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }

                }
            });

            
        },
        do_post_thread: function(item,type) {
            var that = this;
            ns.post({
                id: item.id,
                Type:type,
                action: 'postthreadpraise'
            }, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                        item.ispraised = data.IsPraise;
                        item.praisecount = data.count;
                        item.praisecountdesc = data.count;
                
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_menu();
    api.addEventListener({
        name: 'updatabbs'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
        } else {}
    });
    api.addEventListener({
        name: 'updatedianzan'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
        } else {}
    });
    api.addEventListener({
        name: 'open_bbs_input'
    }, function(ret, err) {
        if (ret) {
            app.selectId=0;
            ns.post({
                action: 'postornot',
            }, function(succeed, data, err) {
                if(succeed){
                    if(data.IsActive){
                        ns.openWin('dl_bbspost_frm', '发帖')
                    }else{
                        api.toast({
                            msg: "你已被禁言",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            });

            
        } else {}
    });
    var pullRefresh = new auiPullToRefresh({
        container: document.querySelector('.aui-refresh-content'),
        triggerDistance: 100
    }, function(ret) {
        if (ret.status == "success") {
            setTimeout(function() {
                app.get_data();
                pullRefresh.cancelLoading(); //刷新成功后调用此方法隐藏
                var header = $api.byId('aui-header');
                $api.css(header, 'display:block');
            }, 1500)
        } else {
            var header = $api.byId('aui-header');
            $api.css(header, 'display:block');
        }
    }, function(ret) {
        if (ret.distance > 0) {
            var header = $api.byId('aui-header');
            $api.css(header, 'display:none');
        }
    })
};
